package com.hundred.item.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.hundred.item.dao.ItemDao;
import com.hundred.item.dao.dataobject.ItemDO;
import com.hundred.item.dao.dataobject.ItemReqDO;
import com.hundred.item.manager.impl.ItemManagerImpl;

public class ItemDaoImpl extends SqlMapClientDaoSupport implements ItemDao {

	private static final Logger logger = LoggerFactory.getLogger(ItemDaoImpl.class);
	
	public long insertItem(ItemDO itemDo) throws SQLException {
		
		return (Long) this.getSqlMapClientTemplate().insert("Item.insertItem", itemDo);

	}

	public int updateItem(ItemDO itemDo) throws SQLException {
		return this.getSqlMapClientTemplate().update("Item.updateItem", itemDo);
	}

	public int deleteItem(long itemId) throws SQLException {
		ItemDO itemDo = new ItemDO();
		itemDo.setItemId(itemId);
		itemDo.setItemStatus(0);
		return this.getSqlMapClientTemplate().update("Item.deleteItem", itemDo);
	}

	public ItemDO getItemById(ItemReqDO itemReq) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("itemId", itemReq.getItemId());
		map.put("tagId", itemReq.getTagId());
		
		logger.error("itemId:"+itemReq.getItemId()+"|"+"tagId:"+itemReq.getTagId());
		
		return (ItemDO) this.getSqlMapClientTemplate().queryForObject("Item.querySingleItem",map);
	}

	public List<ItemDO> getTopItemsList(int status, long startRow, long endRow) {
		Map<String,Long> param = new HashMap<String,Long>();
		param.put("status", (long)(status));
		param.put("startRow", startRow);
		param.put("endRow", endRow);
		@SuppressWarnings("unchecked")
		List<ItemDO> topList = this.getSqlMapClientTemplate().queryForList("Item.getTopItemList", param);
		return topList;
	}

	public long countTotalOnlineItems(int status) {
		return (Long) this.getSqlMapClientTemplate().queryForObject("Item.countTotalOnlineItems",status);
	}

	public ItemDO getHistoryItemById(long itemId) {
		return (ItemDO) this.getSqlMapClientTemplate().queryForObject("Item.querySingleHistoryItem",itemId);
	}

	public List<ItemDO> getInitCacheItems(int status) {
		@SuppressWarnings("unchecked")
		List<ItemDO> itemList = this.getSqlMapClientTemplate().queryForList("Item.cacheInitItem", status);
		return itemList;
	}
}
